package com.virtual.rerijaapps.pet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.melnykov.fab.FloatingActionButton;
import com.mobeta.android.dslv.DragSortListView;
import com.virtual.rerijaapps.pet.adapter.PetBoxAdapter;
import com.virtual.rerijaapps.pet.data.PetData;
import com.virtual.rerijaapps.pet.data.dao.PetDataDao;
import com.virtual.rerijaapps.pet.sound.SoundPlayer;
import com.virtual.rerijaapps.pet.ui.NewEventInclude;
import com.virtual.rerijaapps.pet.utils.DialogHelper;


@EActivity( R.layout.principal_screen )
public class PrincipalScreen extends BaseActivity
{
	
	@ViewById( R.id.principal_list_pet )
	public DragSortListView mPetList;
	
	public PetBoxAdapter mPetAdapter;
	
	public ImageView mHeaderList;
	
	@ViewById( R.id.principal_new_event )
	public NewEventInclude mNewEvent;
	
	private DragSortListView.DragScrollProfile ssProfile =
	        new DragSortListView.DragScrollProfile() 
	{
        @Override
        public float getSpeed( float w, long t ) 
        {
            if ( w > 0.8f ) 
            {
                return ( ( float ) mPetList.getAdapter().getCount() ) / 0.001f;
            } 
            else 
            {
                return 10.0f * w;
            }
        }
    };
    
    private DragSortListView.DropListener onDrop =
            new DragSortListView.DropListener() 
    {
        @Override
        public void drop( int from, int to ) 
        {
            PetData petData = ( PetData ) mPetAdapter.getItem( from );
            mPetAdapter.removePet( petData );
            mPetAdapter.insertPet( petData, to );
            mPetAdapter.notifyDataSetChanged();
        }
    };

    private DragSortListView.RemoveListener onRemove = 
        new DragSortListView.RemoveListener() 
    {
        @Override
        public void remove( int which ) 
        {
            DialogHelper.showDialogRemovePet( PrincipalScreen.this, which );
        }
    };

	@AfterViews
	public void setupPage()
	{
		mAquery.id( R.id.principal_add_pet ).clicked( this, "clickNewEvent" );
		mAquery.id( R.id.principal_logo ).visible().animate( R.anim.anim_down, new AnimationListener() 
		{
			
			@Override
			public void onAnimationStart(Animation animation) 
			{
				SoundPlayer.playSound( PrincipalScreen.this, R.raw.open_app );
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) 
			{
			}
			
			@Override
			public void onAnimationEnd(Animation animation) 
			{
				loadPets();
			}
		} );
	}
	
	public void clickNewEvent( View view )
	{
		mAquery.id( mNewEvent ).visible().animate( R.anim.scale_up_in );
		( ( NewEventInclude ) mAquery.id( mNewEvent ).getView() ).resetFields();
		SoundPlayer.playSound( PrincipalScreen.this, R.raw.pop );
	}
	
	@UiThread
	public void closeEventWindow()
	{
		mAquery.id( mNewEvent ).gone().animate( R.anim.scale_up_out );
		SoundPlayer.playSound( PrincipalScreen.this, R.raw.pop );
	}
	
	@Background
	public void loadPets()
	{
		com.virtual.rerijaapps.pet.data.dao.PetDataDao petDataDao = new com.virtual.rerijaapps.pet.data.dao.PetDataDao( this );
		List<PetData> mPetData = ( ArrayList<PetData> ) petDataDao.listAll();
		
		ArrayList<Integer> positions = new ArrayList<Integer>();
		
		if ( null != mPetData )
		{
			for ( PetData petData : mPetData )
			{
				positions.add( petData.getList_position() );
			}
		}
		
		ArrayList<PetData> mSortPetData = null;
		
		if ( !positions.isEmpty() )
		{
			Object[] positionsInteger = positions.toArray();
			
			mSortPetData = new ArrayList<PetData>();
			
			Arrays.sort( positionsInteger );
			
			for ( Object integer : positionsInteger )
			{
				
				int inte = Integer.parseInt( integer.toString() );
				
				for ( PetData petData : mPetData )
				{
					if ( inte == petData.getList_position() )
					{
						mSortPetData.add( petData );
					}
				}
				
			}
		}
		
		refreshPets( mSortPetData );
	}
	
	@UiThread
	public void refreshPets( ArrayList<PetData> mPetData )
	{
		if ( null == mPetData || mPetData.isEmpty() )
		{
			mAquery.id( R.id.principal_no_pets ).visible().animate( R.anim.rotate );
		}
		else
		{
			findViewById( R.id.principal_no_pets ).setVisibility( View.GONE );
			if ( null == mHeaderList )
			{
				ImageView headerList = new ImageView( this );
				headerList.setScaleType( ScaleType.CENTER );
				headerList.setImageResource( R.drawable.principal_logo );
				AbsListView.LayoutParams headerParams = new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT );
				headerList.setLayoutParams( headerParams );
				mHeaderList = headerList;
				mPetList.addHeaderView( headerList );
			}
			mPetAdapter = new PetBoxAdapter( this, mPetData );
			mPetList.setAdapter( mPetAdapter );
			if ( View.VISIBLE == mAquery.id( R.id.principal_logo ).getView().getVisibility() )
			{
				mAquery.id( R.id.principal_logo ).gone().animate( R.anim.fadeout_short );
			}
			mPetList.setDragScrollProfile( ssProfile );
			mPetList.setDropListener( onDrop );
			mPetList.setRemoveListener( onRemove );
			( ( FloatingActionButton ) mAquery.id( R.id.principal_add_pet ).getView() ).attachToListView( mPetList );
			mAquery.id( mPetList ).animate( R.anim.scale_up_in );
		}
	}
	
	@UiThread
	public void checkPets()
	{
		ArrayList<PetData> mPetData = ( ArrayList<PetData> ) new PetDataDao( this ).listAll();
		confirmCheckPets( mPetData != null && !mPetData.isEmpty() );
	}
	
	public void confirmCheckPets( boolean exist )
	{
		if ( !exist )
		{
			mAquery.id( R.id.principal_no_pets ).visible().animate( R.anim.rotate );
		}
		else
		{
			mAquery.id( R.id.principal_no_pets ).gone();
		}
	}

	@Override
	public void onBackPressed() 
	{
	   if ( View.VISIBLE == mNewEvent.getVisibility() )
	   {
		   closeEventWindow();
	   }
	   else
	   {
		   moveTaskToBack( true );
	   }
	}
	
	@Override
	public void onPause() 
	{
		super.onPause();
	}

	@Override
	public void onResume() 
	{
		super.onResume();
	}
	
	@Override 
	protected void onDestroy() 
	{
		super.onDestroy();
	}
	
}
